package com.anrongtec.zcpt.manager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.base.BaseActivity;
import com.anrongtec.zcpt.module.AppModel;
import com.anrongtec.zcpt.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;
import com.anrongtec.zcpt.util.Util;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

/**
 * 安装记录
 * 
 * @author dongtianhao
 *
 */
public class AppInstallRecordActivity extends BaseActivity {
	private final static int STATUS_DATA_UPDATE = 1;
	private final static int STATUS_DATA_EMPTY = 2;
	private static final long DELAYED_TIME = 500;
	private EmptyWrapper<?> empty;
	private List<AppModel> apps;
	private AppInstallRecordAdapter<AppModel> adapter;
	private RecyclerView rv_app_install_record;
	private StickyRecyclerHeadersDecoration headersDecor;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case STATUS_DATA_UPDATE:
				empty.notifyDataSetChanged();
				break;
			case STATUS_DATA_EMPTY:
				empty.setEmptyView(R.layout.empty_view);
				rv_app_install_record.setAdapter(empty);
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_app_install_record);
		initViews();
		initData();
	}

	@Override
	protected void initViews() {
		rv_app_install_record = (RecyclerView) findViewById(R.id.rv_app_install_record);
		rv_app_install_record.setLayoutManager(new LinearLayoutManager(this));
		// 设置Item增加、移除动画
		rv_app_install_record.setItemAnimator(new DefaultItemAnimator());
	}

	@Override
	protected void initData() {
		apps = new ArrayList<AppModel>();
		adapter = new AppInstallRecordAdapter<AppModel>(this, R.layout.item_app,apps) ;
		empty = new EmptyWrapper<Object>(adapter);
		empty.setEmptyView(R.layout.loading_view);
		headersDecor = new StickyRecyclerHeadersDecoration(adapter);
		rv_app_install_record.addItemDecoration(headersDecor);
		rv_app_install_record.setAdapter(empty);
		adapter.registerAdapterDataObserver(obs);
		// 获取数据
		Util.post(new Runnable() {

			@Override
			public void run() {
//				apps.addAll(Util.getAppsInfo(getApplicationContext()));
				apps.clear();
				apps.addAll(Util.getAppsFromMaket(AppInstallRecordActivity.this));
				
				Collections.sort(apps, new Comparator<AppModel>() {

					@Override
					public int compare(AppModel lhs, AppModel rhs) {
						if (lhs.getFirstInstallTime() > rhs
								.getFirstInstallTime()) {
							return -1;
						}
						if (lhs.getFirstInstallTime() < rhs
								.getFirstInstallTime()) {
							return 1;
						}
						return 0;
					}
				});
				if (apps.isEmpty()) {
					handler.sendEmptyMessageDelayed(STATUS_DATA_EMPTY,
							DELAYED_TIME);
				} else {
					handler.sendEmptyMessageDelayed(STATUS_DATA_UPDATE,
							DELAYED_TIME);
				}
			}
		});
	}
	
	RecyclerView.AdapterDataObserver obs = new RecyclerView.AdapterDataObserver() {
		@Override
		public void onChanged() {
			headersDecor.invalidateHeaders();
		}
	};

	@Override
	protected void onCreateCustomToolBar(Toolbar toolbar) {
		toolbar.setTitle(getString(R.string.title_activity_install_record));
		super.onCreateCustomToolBar(toolbar);
	}

	@Override
	protected void onDestroy() {
		adapter.unregisterAdapterDataObserver(obs);
		super.onDestroy();
	}
}
